﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bungalow.Spider.Elements;
using System.Xml;

namespace Bungalow.Spider
{
    public class Serializer
    {
        public SpiderView mView;
        public SpiderView View {
            get{
                return this.mView;
            }
        }
        public String capitalize(String source)
	    {
		    String bob = "";
		    foreach (String str in source.Split(' ')) {
			    bob+=(str.Substring(0, 1).ToUpper());
			    bob+=(str.Substring(1).ToLower());
			    bob+=(" ");
		    }
		    return bob;
	    }
        public element deserialize(XmlNode c)
	    {
		
		
		    if (c.GetType() ==  typeof(XmlElement))
		    {
			    XmlElement elm = (XmlElement)c;
		
			    try {
				    element _elm = (element)Type.GetType("com.krikelin.spider."+elm.Name.ToLower()).GetConstructor(new System.Type[]{mView.GetType()}).Invoke(new Object[]{mView});
			
				    for(int i=0; i <elm.Attributes.Count; i++)
				    {
					    XmlNode attrib = elm.Attributes[i];
					    String val = attrib.InnerText;
					    String attribute = attrib.Name;
					    String methodName = capitalize(attribute).Trim();
					    if(attribute.StartsWith("src"))
					    {
						    int ec = 3;
					    }
					    try
					    {
						
						    int valN = 0;
                           int.TryParse(val, out valN);
                            Object s = new Object();
                            if(valN > 0) {
                                Type.GetType("com.krikelin.spider."+elm.Name.ToLower()).GetMethod("set"+methodName).Invoke(_elm, new Object[]{valN});
					        } else {
						        Type.GetType("com.krikelin.spider."+elm.Name.ToLower()).GetMethod("set"+methodName).Invoke(_elm, new Object[]{val});
					        }
                        }
					         
                        catch(Exception e)
					    {
						    try{
							    _elm.GetType().GetMethod("set"+methodName).Invoke(_elm, new Object[]{val});
						    }
						    catch(Exception e2){
							
						    }
					    }
					    // set data of method
					
					
					
				
					
				    }
                    
                        XmlNode x= ((XmlElement)elm).FirstChild;
					    if(x.GetType() == typeof(XmlText))
						    if(x.InnerText != null)
						    {
							    _elm.GetType().GetMethod("setData").Invoke(_elm, new Object[]{x.InnerText});
							
						    }
				   
				    XmlNodeList childXmlNodes = elm.ChildNodes;
				    int length = childXmlNodes.Count;
				    for(int j=0; j < length; j++)
				    {
					
					    XmlNode welm = childXmlNodes[j];
					    XmlNode parent = welm.ParentNode;
					
					    if(welm.GetType() ==  typeof(XmlElement))
					    {
						    if(welm.ParentNode!=(elm))
							    continue;
						    element f = deserialize(welm);
						    if(f != null)
						    {
				    //			f.setBounds(new Rectangle(0,0,_elm.getWidth(),_elm.getHeight()));
							    _elm.getChildren().Add(f);
						    }
					    }
					    }
				    _elm.onLoad((Object[])null);
				    return _elm;
			    } catch (Exception e) {
				    // TODO Auto-generated catch block
				  
			    }
			
			
		    }
	
		    return null;
	    }
    }
}
